package com.study.bookstore.service.impl;

import com.alibaba.fastjson.util.TypeUtils;
import com.study.bookstore.service.CommentService;
import com.study.bookstore.domain.Comment;
import com.study.bookstore.repository.CommentRepository;
import com.study.bookstore.service.dto.DtoComment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Service Implementation for managing Comment.
 */
@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    private final Logger log = LoggerFactory.getLogger(CommentServiceImpl.class);

    private final CommentRepository commentRepository;

    public CommentServiceImpl(CommentRepository commentRepository) {
        this.commentRepository = commentRepository;
    }

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * Save a comment.
     *
     * @param comment the entity to save
     * @return the persisted entity
     */
    @Override
    public Comment save(Comment comment) {
        log.debug("Request to save Comment : {}", comment);
        comment.setpComment("评论者");
        comment.setCommentTime(ZonedDateTime.now());
        return commentRepository.save(comment);
    }

    /**
     * Get all the comments.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<Comment> findAll(Pageable pageable) {
        log.debug("Request to get all Comments");
        return commentRepository.findAll(pageable);
    }

    /**
     * Get one comment by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Comment findOne(Long id) {
        log.debug("Request to get Comment : {}", id);
        return commentRepository.findOne(id);
    }

    /**
     * Delete the comment by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Comment : {}", id);
        commentRepository.delete(id);
    }

    @Override
    public List<DtoComment> getComment(Long id) throws Exception {
        List<DtoComment> comments=getCommentByComment("评论者");
        List<DtoComment> result=new ArrayList<>();
        for (DtoComment comment: comments) {
            if (comment.getBookId().equals(id)){
                result.add(comment);
            }
        }
        return result;
    }

    @Override
    public List<DtoComment> getCommentByComment(String p_comment) throws Exception {
        String sql="SELECT a.*,(SELECT COUNT(*) FROM COMMENT WHERE (p_comment=a.jhi_comment)) AS leaf\n" +
            " FROM COMMENT a WHERE a.p_comment='"+p_comment+"' ";
        List<Map<String,Object>> queryList= jdbcTemplate.queryForList(sql);
        List<DtoComment> list=new ArrayList<>();
        for ( Map<String,Object> item:queryList) {
            DtoComment dtoComment=new DtoComment();
            dtoComment.setContent(TypeUtils.castToString(item.get("content")));
            dtoComment.setComment(TypeUtils.castToString(item.get("commentator")));
            dtoComment.setBookId(TypeUtils.castToLong(item.get("book_id_id")));
            dtoComment.setCommentTime(TypeUtils.castToString(item.get("comment_time")));
            if (TypeUtils.castToInt(item.get("leaf"))>0){
                dtoComment.setLeaf(true);
            }
            else {
                dtoComment.setLeaf(false);
            }
            if (dtoComment.getLeaf()){
                dtoComment.setChildren(getCommentByComment(dtoComment.getComment()));
            }
            list.add(dtoComment);
        }
        return list;
    }
}
